package com.niit.onlinemusic.controller;

import com.niit.onlinemusic.common.R;
import com.niit.onlinemusic.model.domain.Singer;
import com.niit.onlinemusic.model.request.SingerRequest;
import com.niit.onlinemusic.service.SingerService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

/**
 * 歌手控制器
 *
 * @author zuwei522
 * @since 2025/9/17 16:24
 */
@Slf4j
@RequestMapping("singer")
@RestController
public class SingerController {

    private final SingerService singerService;

    public SingerController(SingerService singerService) {
        this.singerService = singerService;
    }

    @GetMapping
    public R allSingers() {
        return singerService.getAllSingers();
    }

    /**
     * 添加歌手
     *
     * @param singerRequest 添加歌手请求体
     * @return R
     * @author zuwei522
     * @since 2025/9/23 22:15
     */
    @PostMapping("add")
    public R addSinger(@RequestBody SingerRequest singerRequest) {
        Singer singer = new Singer();
        BeanUtils.copyProperties(singerRequest, singer);
        //给出默认头像,后续管理员可以在线修改
        singer.setPic("https://java-pp.oss-cn-hangzhou.aliyuncs.com/img/singerPic/user.jpg");
        log.info("添加歌手: {}", singer);
        if (singerService.save(singer)) {
            return R.ok();
        } else {
            return R.error("添加失败");
        }
    }

    /**
     * 删除歌手
     *
     * @param id 歌手id
     * @return R
     * @author zuwei522
     * @since 2025/9/27 21:50
     */
    @DeleteMapping("delete")
    public R deleteSinger(@RequestParam Integer id) {
        log.info("删除歌手: {}", id);
        if (singerService.removeById(id)) {
            return R.ok();
        } else {
            return R.error("删除失败");
        }
    }

    /**
     * 更新歌手
     *
     * @param singerRequest 更新歌手请求体
     * @return R
     * @author zuwei522
     * @since 2025/9/27 21:52
     */
    @PostMapping("update")
    public R updateSinger(@RequestBody SingerRequest singerRequest) {
        Singer singer = new Singer();
        BeanUtils.copyProperties(singerRequest, singer);
        log.info("更新歌手: {}", singer);
        if (singerService.updateById(singer)) {
            return R.ok();
        } else {
            return R.error("更新失败");
        }
    }

}
